#include "TransformMover.hpp"

using namespace Typhoon;

TransformMover::TransformMover() {
	input = 0;
	isRotating = false;
	Speed = 0.02f;
}

TransformMover::~TransformMover() {
	if (input) {
		input->Down -= event_handler(this, &TransformMover::Down);
		input->Up -= event_handler(this, &TransformMover::Up);
	}
}

void TransformMover::SetInputManager( InputManager* input )
{
	this->input = input;
	input->Down += event_handler(this, &TransformMover::Down);
	input->Up += event_handler(this, &TransformMover::Up);
}


void TransformMover::Down( InputManager::EventData event )
{
	if (event.Index!=0) return;

	startPosition = event.Position;
	isRotating = true;
}

void TransformMover::Up( InputManager::EventData event )
{
	if (event.Index!=0) return;
	isRotating = false;
}

void TransformMover::Update(float dt) {
	if (!isRotating) { return; }

	Vector2 delta = input->GetPosition(1) - startPosition;

	GameObjects& list = GetGameObjects();

	for (GameObjects::iterator it = list.begin(); it!=list.end(); ++it) {
		Transform* transform = (*it)->GetComponent<Transform>();

		Vector3 vector = transform->World.GetValue()->TransformVector(Vector3(delta.x * dt * Speed,0,delta.y * dt * Speed));

		transform->Position += vector;
	}
}
